﻿using AutoMapper;
using ERPSystem.Common.Domains.UserCommon;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;

namespace RBACWrite.API.Command.Roless
{
    public class RolesCreateHandler : IRequestHandler<RolesCreateCommon, Result>
    {
       private readonly IBaseRepository<Roles> rolesRep;
       private readonly IMapper mapper;

        public RolesCreateHandler(IBaseRepository<Roles> rolesRep, IMapper mapper)
        {
            this.rolesRep = rolesRep;
            this.mapper = mapper;   
        }

        public async Task<Result> Handle(RolesCreateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                Roles roles = mapper.Map<Roles>(request);
                roles.CreatedOwnerId = "张三";
                roles.CreatedTime = DateTime.Now;
                //roles.UpdatedOwnerId = "";
                //roles.UpdatedTime = null;
                //roles.DeletedOwnerId = "";
                //roles.DeletedTime = null;
                //roles.Auditor = "";
                //roles.AuditDate = null;
                var n = await rolesRep.Add(roles);

                result.Code = (int)CommonErrorCode.Success;
                result.Msg = "添加成功";
            }
            catch (Exception)
            {
                result.Msg = "添加失败";
                throw;
            }
            return result;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

    }
}
